package com.media.util;

import java.sql.*;

public class DBUtil {
    public static Connection getConnection(){
        Connection connection = null;
        try {
            //加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            //建立数据库连接
            String url = "jdbc:mysql://localhost:3306/book";
            connection = DriverManager.getConnection(url,"root","root");
        }catch (Exception e){
            e.printStackTrace();
        }
        return connection;
    }

    public static void close(Connection connection, PreparedStatement ps, ResultSet rs){
        //关闭数据库连接

        if(rs != null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        if(ps != null){
            try {
                ps.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }

        if(connection != null){
            try {
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }

    /**
     * MVC Model 模型层  访问数据库  View 视图层 显示页面内容的  Controller 控制器 负责页面请求跳转的
     *
     * dao Data Access Object 数据访问对象
     *
     *
     */
}

